package com.github.chxiaowu.service;

import static com.mongodb.client.model.Filters.eq;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.chxiaowu.mongo.dto.PersonDto;

@Service
public class PersonService extends BaseService {

	static final String COLL_NAME = "person";

	@Override
	protected String collName() {
		return COLL_NAME;
	}

	/**
	 * 根据id获取一条记录
	 * @param id
	 * @return
	 */
	public PersonDto getPerson(String id) {
		if (StringUtils.isEmpty(id)) {
			return null;
		}
		return mongoHelper.getCollection(collName(), PersonDto.class).find(eq("_id", new ObjectId(id))).first();
	}

	/**
	 * 根据city获取所有记录
	 * @param city
	 * @return
	 */
	public List<PersonDto> getPersonList(String city) {
		if (StringUtils.isEmpty(city)) {
			return null;
		}
		List<PersonDto> result = new ArrayList<>();
		mongoHelper.getCollection(collName(), PersonDto.class).find(eq("addr.city", city)).into(result);
		return result;
	}

}
